/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.dbg.model.content {
	import com.googlecode.milib.base.IMLBroadcasterListener;
	import com.googlecode.milib.base.MLBroadcastClass;
	import com.googlecode.milib.tools.MLEventsReflector;
	import com.googlecode.milib.vo.MLEventInfo;

	import flash.utils.Dictionary;

	/**
	 * @author Marek Brun
	 */
	public class MLDBGValuesListModel extends MLBroadcastClass implements IMLBroadcasterListener {

		//DATA:	MLDBGValuesListModelItem //added item
		public const event_ItemAdded : String = 'ItemAdded';
		
		//DATA:	MLDBGValuesListModelItem //removed item
		public const event_ItemRemoved : String = 'ItemRemoved';
		
		//DATA:	MLDBGValuesListModelItem //added item
		public const event_ItemChanged : String = 'ItemChanged';
		
		private var values:Dictionary;
		private var valuesEvents:MLEventsReflector;		protected var isCount:Boolean;
		private var count:Number=0;
		
		public function MLDBGValuesListModel(isCount:Boolean=true):void {
			super();
			this.isCount=isCount;
			values=new Dictionary();
			valuesEvents=new MLEventsReflector(true);
			valuesEvents.addListener(this);
		}
		/*abstract*/ public function refresh():void {}
		
		/**
		 * @param values Array of Array //[name:String, value:*=null]
		 */
		public function setValues(values:Array):void {
			clear();
			var loopObj:Array;
			var i:uint;
			for(i=0;i<values.length;i++){
				loopObj=values[i];
				addValue(loopObj[0], loopObj[1]);
			}
		}
		
		/**
		 * @param name if name alredy exists and value is changed
		 * value is only changing (no new value is added, names is unique)
		 */
		public function addValue(name:*, value:*=null, index:Number=NaN):void {
			var isNew:Boolean=false;
			if(isNaN(index)){ index=count++; }
			if(!values[name]){
				values[name]=new MLDBGValuesListModelItem(isCount, index);
				valuesEvents.addObject(values[name]);
				isNew=true;
			}
			var valueObj:MLDBGValuesListModelItem=values[name];
			valueObj.setName(name);			valueObj.setValue(value);
			if(isNew){
				broadcastEvent(event_ItemAdded, valueObj);
			}
		}
		
		protected function removeItem(item:MLDBGValuesListModelItem):void {
			valuesEvents.removeObject(item);
			delete values[item];
			broadcastEvent(event_ItemRemoved, item);
		}
		
		public function clear():void {
			var i:uint;
			if(valuesEvents.getAddedObjects().length){
				var arr:Array=valuesEvents.getAddedObjects();
				var loopItem:MLDBGValuesListModelItem;
				for(i=0;i<arr.length;i++){
					loopItem=arr[i];
					removeItem(loopItem);
				}
				
				valuesEvents.clear();
				this.values=new Dictionary();
			}
		}
		
		public function toArray():Array {
			var arr:Array=[];
			for(var i:* in values){
				arr.push(values[i]);
			}
			arr.sortOn('index', Array.NUMERIC);
			return arr;
		}
		
		public function toValueArray():Array {
			var arr:Array=[];
			for(var i:* in values){
				arr.push(MLDBGValuesListModelItem(values[i]).getValue());
			}
			return arr;
		}
		
//********************************************************************************************
//		events for MLDBGValuesListModel
//********************************************************************************************
		public function onEvent(ev:MLEventInfo):void {
			switch(ev.hero){
				case valuesEvents:
					if(ev.event!=valuesEvents.event_EventReflection){ break; }
					ev=ev.data;
					var hero:MLDBGValuesListModelItem=MLDBGValuesListModelItem(ev.hero);
					switch(ev.event) {
						case hero.event_Changed:
							broadcastEvent(event_ItemChanged, hero);
						break;
					}
				break;
			}
		}
		
	}
}
